package cc.vv.baselibrary.http;

import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import cc.vv.baselibrary.bean.base.BaseResponseObj;
import cc.vv.lkbasecomponent.http.lib.CallBack;

/**
 * http响应各生命周期
 * Created by 025 on 2018/7/12.
 */

public class BaseHttpCallBack<T extends BaseResponseObj> extends CallBack<T> {

    @Override
    public void onStart(String url, boolean isLoading) {
        super.onStart(url, isLoading);
        BaseHttpResponseObj baseHttpResponseObj = new BaseHttpResponseObj();
        baseHttpResponseObj.setResponseType(BaseHttpResponseKey.RESPONSE_TYPE_START);
        baseHttpResponseObj.setUrl(url);
        baseHttpResponseObj.setLoading(isLoading);
        EventBus.getDefault().post(baseHttpResponseObj);
    }

    @Override
    public void onSuccess(String url, T obj) {
        super.onSuccess(url, obj);
        BaseHttpResponseObj baseHttpResponseObj = new BaseHttpResponseObj();
        baseHttpResponseObj.setResponseType(BaseHttpResponseKey.RESPONSE_TYPE_SUCCESS);
        baseHttpResponseObj.setUrl(url);
        baseHttpResponseObj.setData(obj);
        EventBus.getDefault().post(baseHttpResponseObj);
    }

    @Override
    public void onSuccess(String url, String resultText, T obj) {
        super.onSuccess(url, resultText, obj);
        BaseHttpResponseObj baseHttpResponseObj = new BaseHttpResponseObj();
        baseHttpResponseObj.setResponseType(BaseHttpResponseKey.RESPONSE_TYPE_SUCCESS_RESULT_TEXT);
        baseHttpResponseObj.setUrl(url);
        baseHttpResponseObj.setResultText(resultText);
        baseHttpResponseObj.setData(obj);
        EventBus.getDefault().post(baseHttpResponseObj);
    }

    @Override
    public void onFailure(String url, boolean isLoading, String exceptionStr) {
        super.onFailure(url, isLoading, exceptionStr);
        BaseHttpResponseObj baseHttpResponseObj = new BaseHttpResponseObj();
        baseHttpResponseObj.setResponseType(BaseHttpResponseKey.RESPONSE_TYPE_FAILURE);
        baseHttpResponseObj.setUrl(url);
        baseHttpResponseObj.setLoading(isLoading);
        baseHttpResponseObj.setExceptionStr(exceptionStr);
        EventBus.getDefault().post(baseHttpResponseObj);
    }

    @Override
    public void onCancel(String url, boolean isLoading) {
        super.onCancel(url, isLoading);
        BaseHttpResponseObj baseHttpResponseObj = new BaseHttpResponseObj();
        baseHttpResponseObj.setResponseType(BaseHttpResponseKey.RESPONSE_TYPE_CANCEL);
        baseHttpResponseObj.setUrl(url);
        baseHttpResponseObj.setLoading(isLoading);
        EventBus.getDefault().post(baseHttpResponseObj);
    }

    @Override
    public void onFinish(String url, int resultCode, boolean isLoading) {
        super.onFinish(url, resultCode, isLoading);
        BaseHttpResponseObj baseHttpResponseObj = new BaseHttpResponseObj();
        baseHttpResponseObj.setResponseType(BaseHttpResponseKey.RESPONSE_TYPE_FINISH);
        baseHttpResponseObj.setUrl(url);
        baseHttpResponseObj.setResultCode(resultCode);
        baseHttpResponseObj.setLoading(isLoading);
        EventBus.getDefault().post(baseHttpResponseObj);
    }
}
